//jQuery常用函数封装
//本页面功能模仿或COPY部分DuxCMS 网址:http://www.duxcms.com
$.ajaxSetup({
    cache : false
});
(function ($){
//#################
/*
$.fn.YouName = function (options){
    var defaults = {}
    var options = $.extend(defaults,options);
    this.each(function (){
        ....Code....
    });
};
*/
//Jquery全选插件 全选，反选和取消按钮功能
$.fn.cpCheck = function(a) {
    var a = a || "on";
    return this.each(function() {
        switch (a) {
          case "on":
            this.checked = true;
            break;
          case "off":
            this.checked = false;
            break;
          case "toggle":
            this.checked = !this.checked;
            break;
        }
    });
};
//页面删除操作
$.fn.cpDelete = function (){
    var url = $(this).attr("url");
    var css = this;
    var win = $.dialog.top;
    return this.each(function() {
        art.dialog.confirm("确认要删除本资源?", function() {
            $.get(url, function(c) {
                if (c != 1) {
                    win.art.dialog({content:c,lock:true,fixed:true,time:1});
                    return False;
                }
                $(css).parents("tr").remove();
                $(css).parent().remove();
            });
        }, function() {});
    });
};
//ajax操作
$.fn.cpGet = function (){
    var url = $(this).attr("url");
    var tip = $(this).attr("tip");
    var css = this;
    var win = $.dialog.top;
    return this.each(function() {
        art.dialog.confirm(tip, function() {
            $.get(url,function(data) {
                if (data.status == "1") { //直接跳转
                    if (data.url) {
                        location.replace(data.url)
                    } else {
                        location.reload()
                    } 
                }else if(data.status == "2"){//替换内容
                    $(css).parent().html(data.info);
                }else if(data.status == "3"){//提示对话窗窗口
                    art.dialog.confirm(data.info, function() {
                        if (data.url) {
                            location.replace(data.url)
                        } else {
                            location.reload()
                        } 
                    }, function(){});
                }else{//弹窗提示
                    win.art.dialog({content:data.info,lock:true,fixed:true,time:2});
                }
            },"json"); 
        }, function() {});
    });
};
//弹窗窗口
$.fn.cpWindows= function (){
    var win = $.dialog.top;
    var url = $(this).attr("url");
    var title = $(this).attr("title");
    return this.each(function() {
        $.dialog.open(url,{opacity:0.20,title:title,lock:true,fixed:true})
    });
};
//上传窗口
$.fn.cpUploads= function (){
    var win = $.dialog.top;
    var input_id = $(this).attr("uid");
    var url = rootUrl + 'index.php?r=cms/files/manage&id=' + input_id;
    return this.each(function() {
        $.dialog.open(url,{opacity:0.20,title:'文件上传管理器',lock:true,fixed:true})
    });
};
//编辑器调用
$.fn.cpEditor = function (options){
    var defaults = {
        uploadUrl : rootUrl + 'index.php?r=cms/files/upload',
        fileUrl : '',
        config : {}
    }
    var options = $.extend(defaults,options);
    this.each(function (){
        var id = this;
        var editorConfig ={
            allowFileManager : false,
            uploadJson:options.uploadUrl,
            fileManagerJson:options.fileUrl,
            afterBlur : function (){
                this.sync();
            },
            width : '100%',
            height: '450px'
        };
        editorConfig = $.extend(editorConfig,options.config);
        var editor = KindEditor.create(id,editorConfig);
    });
};
//常用编辑器
$.fn.cpEditorSimple = function (options){
    var defaults = {
        uploadUrl : rootUrl + 'index.php?r=cms/files/upload',
        fileUrl : '',
        config : {}
    }
    var options = $.extend(defaults,options);
    this.each(function (){
        var id = this;
        var editorConfig ={
            allowFileManager : false,
            uploadJson:options.uploadUrl,
            fileManagerJson:options.fileUrl,
            items : ['source','plainpaste','wordpaste','fontname','fontsize','|','forecolor','hilitecolor','bold', 'italic','underline','removeformat','|', 'justifyleft','justifycenter','justifyright','insertorderedlist','insertunorderedlist','|','emoticons', 'image','table','link','clearhtml','fullscreen'],
            afterBlur : function (){this.sync();}
        };
        editorConfig = $.extend(editorConfig,options.config);
        var editor = KindEditor.create(id,editorConfig);
    });
};
//颜色
$.fn.cpColor = function (options){
    var defaults = {}
    var options = $.extend(defaults, options);
    this.each(function (){
        $(this).soColorPacker();
    });
};
//表单处理
$.fn.cpForm = function (options){
    var defaults = {
        ajaxpost : null
    }
    var options = $.extend(defaults, options);
    this.each(function (){
        var form = this;
        if (options.ajaxpost == null || options.ajaxpost == ''){
            $(form).Validform({
                btnSubmit: ".submit",showAllError: true,tiptype: 4,ajaxPost: true,postonce:true,
                callback: function(data) {
                    if (data.status == "1") {
                        art.dialog.confirm(data.info,function() {
                            var winone = artDialog.open.origin;
                            var wintwo = art.dialog.open.origin;
                            if (data.url) {
                                wintwo.location.replace(data.url)
                            } else {
                                wintwo.location.reload()
                            }
                        },function() {
                            $.dialog.through({content:"你取消了操作!",lock:true,fixed:true,time:1});
                        })
                    } else {
                        $.dialog.through({id: "vform",time: 2,lock: true,content:data.info})
                    }
                }
            })
        }else{
            $(form).Validform({
                btnSubmit: ".submit",showAllError: true,tiptype: 4
            })
        }
    });
};
//引入提交表单
$.fn.cpFormPage = function (options){
    var defaults ={
        ajaxpost : null
    }
    var options = $.extend(defaults, options);
    this.each(function (){
        var form = this;
        form = $(form);
        //表单处理
        form.cpForm({
            ajaxpost:options.ajaxpost
        });
        //编辑器
        if ($(".ui-editor").length > 0){
            form.find('.ui-editor').cpEditor();
        }
        //常用编辑器
        if ($(".ui-editor-simple").length > 0){
            form.find('.ui-editor-simple').cpEditorSimple();
        }        
        //时间选择器
        if ($(".ui-date").length > 0){
            form.find('.ui-date').calendar({
                format : 'yyyy-MM-dd HH:mm:ss'
            });
        } 
        //颜色选择器
        if ($(".ui-color").length > 0){
            form.find('.ui-color').cpColor();
        }       
        //上传附件
        if ($(".ui-upload").length > 0){
            $(".ui-upload").click(function(){
                $(this).cpUploads();
            });
        }
    });
};
//#################
})(jQuery);